package com.shuma.product.controller;

import com.shuma.common.vo.R;
import com.shuma.product.utils.QiniuUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@CrossOrigin
public class ProductController {

    @Autowired
    private QiniuUtils qiniuUtils;

    // 图片上传接口
    @PostMapping("uploadImage")
    public R uploadImage (MultipartFile file) {
        // 原始文件名称 比如aa.png
        String originalFilename = file.getOriginalFilename();
        // 唯一的文件名称
        String fileName = UUID.randomUUID().toString() + "." + StringUtils.substringAfterLast(originalFilename, ".");
        // 上传文件 上传到七牛云
        boolean upload = qiniuUtils.upload(file, fileName);
        if (upload) {
            return R.ok(QiniuUtils.url + fileName);
        }
        return R.fail(403, "上传图片失败");
    }
}
